package com.lonwin.inspectionmanagement.mvp.presenter;

import android.app.Application;
import android.util.ArrayMap;

import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.jess.arms.utils.DataHelper;
import com.lonwin.inspectionmanagement.app.AppConstants;
import com.lonwin.inspectionmanagement.app.utils.DataUtil;
import com.lonwin.inspectionmanagement.app.utils.RxUtils;
import com.lonwin.inspectionmanagement.mvp.contract.LookPatrolRecordContract;
import com.lonwin.inspectionmanagement.mvp.model.api.Api;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.ConfigBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.SignSuccessBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.XcjlbInfoBean;

import java.io.File;
import java.util.Map;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 12/25/2018 00:19
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class LookPatrolRecordPresenter extends BasePresenter<LookPatrolRecordContract.Model, LookPatrolRecordContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;
    ConfigBean configBean;
    @Inject
    public LookPatrolRecordPresenter(LookPatrolRecordContract.Model model, LookPatrolRecordContract.View rootView) {
        super(model, rootView);
        configBean=DataHelper.getDeviceData(mRootView.getMyActivity(),AppConstants.EXTRA_CONFIG_DATA);

    }

    /**
     * 报送给河长签批和归档巡查记录表
     * @param type
     * @param maps
     */
    public void submittedXcjlb(String type,Map<String,String> maps)
    {
        String url="";
        if (type.equals("submittedXcjlb"))
        {
            //发送给河长签批 submittedXcjlb
            url+=DataUtil.getJNUrl(configBean)+"/hzxh/xcrecordController/submittedXcjlb";
        }else {
            //发送给河长办存档 neatenXcjlb
            //状态是2的记录表才能归档
            url+=DataUtil.getJNUrl(configBean)+"/hzxh/xcrecordController/neatenXcjlb";
        }
        mModel.submittedXcjlb(url,maps).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<XcjlbInfoBean>(mErrorHandler) {
                    @Override
                    public void onNext(XcjlbInfoBean xcjlbInfoBean) {
                    mRootView.submittedXcjlbSuccess(xcjlbInfoBean.getData(),type);
                    }
                });
    }

    /**
     * 获取巡查记录表详情
     * @param xcjlbid
     */
     public void getXcjlbById(String xcjlbid)
     {
         Map<String,Object> maps=new ArrayMap<>();
         maps.put("xcjlbid",xcjlbid);
         mModel.getXcjlbById(DataUtil.getJNUrl(configBean)+"/hzxh/xcrecordController/getXcjlbById",maps)
                 .compose(RxUtils.applySchedulers(mRootView))
                 .subscribe(new ErrorHandleSubscriber<XcjlbInfoBean>(mErrorHandler) {
                     @Override
                     public void onNext(XcjlbInfoBean xcjlbInfoBean) {
                       mRootView.getXcjlbByIdSuccess(xcjlbInfoBean.getData());
                     }
                 });
     }
     public void uploadFile(String path){
         File file=new File(path);
         if (file.exists()){
             mModel.uploadFile(DataUtil.getFileUrl(getConfigBean()),file).map(new Function<String, Map<String,String>>() {
                 @Override
                 public Map<String, String> apply(String s) throws Exception {
                     Map<String,String> params=mRootView.getSignParams();
                     params.put("hzclqm",s);
                     return params;
                 }
             }).flatMap(new Function<Map<String, String>, Observable<SignSuccessBean>>() {
                 @Override
                 public Observable<SignSuccessBean> apply(Map<String, String> stringStringMap) throws Exception {
                     return mModel.signByHeZhang(DataUtil.getJNUrl(configBean)+"/hzxh/xcrecordController/xcjlbSign",stringStringMap);
                 }
             }).compose(RxUtils.applySchedulers(mRootView)).subscribe(new ErrorHandleSubscriber<SignSuccessBean>(mErrorHandler) {
                 @Override
                 public void onNext(SignSuccessBean signSuccessBean) {
                    mRootView.signSuccess();
                 }
             });
         }
     }

    public ConfigBean getConfigBean() {
        return configBean;
    }

    public void setConfigBean(ConfigBean configBean) {
        this.configBean = configBean;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
